﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class viewAllMembers : System.Web.UI.Page
    {
        protected int currentPageNumber = 1;
        private const int MEMBER_PAGE_SIZE = 5;
        private const int MEMBER_PER_ROW = 2;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }


        private void BindData()
        {

            MemberAL memberAL = new MemberAL();
            List<MemberDTO> allMembers = memberAL.GetAllMembers().OrderBy(a => a.FullName ).ToList();
            List<MemberDTO> allMembersOfOnePage = new List<MemberDTO>();
            //gvViewAllMembers.DataSource = allMembers;
            //gvViewAllMembers.DataBind();
            string queryString = Request.QueryString.ToString();

            // get the total rows 
            double totalRows = (int)allMembers.Count;

            //if (queryString == "")
            //{
            //    lblAllMembPage.Text = "";
            //    lblAllMembOf.Text = "";
            //    lblAllMembTotalPage.Text = "";
            //    lblAllMembCurrPage.Text = "";
            //    btnAllMembFirst.Visible = false;
            //    btnAllMembPrev.Visible = false;
            //    btnAllMembNext.Visible = false;
            //    btnAllMembLast.Visible = false;

            //}
           // else
           // {
                lblAllMembPage.Text = "Displaying all of the Colombo PPA members - Page ";
                lblAllMembOf.Text = "  of  ";
                lblAllMembTotalPage.Text = CalculateTotalPages(totalRows).ToString();
                lblAllMembCurrPage.Text = currentPageNumber.ToString();
                //btnAllMembFirst.Visible = true;
                //btnAllMembPrev.Visible = true;
                //btnAllMembNext.Visible = true;
                //btnAllMembLast.Visible = true;
           // }

            //lblAllAthleticTotalPage.Text = CalculateTotalPages(totalRows).ToString();
            int totalPageNumber = lblAllMembTotalPage.Text == "" ? 0 : Convert.ToInt32(lblAllMembTotalPage.Text);

            //lblAllMembTotalPage.Text = CalculateTotalPages(totalRows).ToString();

            //lblAllMembCurrPage.Text = currentPageNumber.ToString();

            if (totalPageNumber == 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllMembFirst.Enabled = false;
                    btnAllMembPrev.Enabled = false;
                    btnAllMembNext.Enabled = false;
                    btnAllMembLast.Enabled = false;
                    allMembersOfOnePage = allMembers;
                }

            }
            else if (totalPageNumber > 1)
            {
                if (currentPageNumber == 1)
                {
                    btnAllMembFirst.Enabled = false;
                    btnAllMembPrev.Enabled = false;
                    btnAllMembNext.Enabled = true;
                    btnAllMembLast.Enabled = true;
                    allMembersOfOnePage = allMembers.GetRange(0, MEMBER_PAGE_SIZE * MEMBER_PER_ROW);
                }
                else if (currentPageNumber < totalPageNumber && currentPageNumber > 1)
                {
                    btnAllMembFirst.Enabled = true;
                    btnAllMembPrev.Enabled = true;
                    btnAllMembNext.Enabled = true;
                    btnAllMembLast.Enabled = true;
                    allMembersOfOnePage = allMembers.GetRange(((currentPageNumber - 1) * MEMBER_PER_ROW * MEMBER_PAGE_SIZE) , MEMBER_PAGE_SIZE * MEMBER_PER_ROW);

                }
                else if (currentPageNumber == totalPageNumber)
                {
                    btnAllMembFirst.Enabled = true;
                    btnAllMembPrev.Enabled = true;
                    btnAllMembNext.Enabled = false;
                    btnAllMembLast.Enabled = false;
                    allMembersOfOnePage = allMembers.GetRange(((currentPageNumber - 1) * MEMBER_PER_ROW * MEMBER_PAGE_SIZE) , (allMembers.Count - (currentPageNumber - 1) * MEMBER_PER_ROW * MEMBER_PAGE_SIZE));
                }
            }

          
            gvViewAllMembers.DataSource = allMembersOfOnePage;
            gvViewAllMembers.DataBind();


        }

        private int CalculateTotalPages(double totalRows)
        {
            int totalPages = (int)Math.Ceiling(totalRows / (MEMBER_PAGE_SIZE * MEMBER_PER_ROW));

            return totalPages;
        }

        protected void ChangePage(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    currentPageNumber = 1;
                    break;

                case "Previous":
                    currentPageNumber = Int32.Parse(lblAllMembCurrPage.Text) - 1;
                    break;

                case "Next":
                    currentPageNumber = Int32.Parse(lblAllMembCurrPage.Text) + 1;
                    break;
                case "Last":
                    currentPageNumber = Int32.Parse(lblAllMembTotalPage.Text);
                    break;
            }


            BindData();
        }
        protected void gvViewAllMembers_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void gvViewAllMembers_PageIndexChanged(object sender, EventArgs e)
        {
           
        }

        protected void gvViewAllMembers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvViewAllMembers.PageIndex = e.NewPageIndex;
            gvViewAllMembers.DataBind();
        }
    }
}
